package cc.cix.hq.org.listener;

import cc.cix.hq.common.core.entity.constant.AuthEventbus;
import cc.cix.hq.common.core.entity.org.LoginLog;
import cc.cix.hq.common.core.entity.org.event.LoginEvent;
import cc.cix.hq.org.service.ILoginLogService;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class LoginListener {

    private final ILoginLogService loginLogService;

    /**
     * 用户登录监听
     *
     * @param event 用户
     */
    @KafkaListener(topics = AuthEventbus.LOGIN)
    public void receive(LoginEvent event) {
        log.info("用户登录监听-user={}", JSONUtil.toJsonStr(event));

        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(event.getUsername());
        loginLog.setLoginTime(event.getLoginTime());
        loginLog.setIp(event.getIp());
        loginLogService.save(loginLog);
    }

}

